package cn.linjianhui.cluster.dao;

import cn.linjianhui.cluster.pojo.RedPacket;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Repository;

@Repository
public interface RedPacketDao {

    /**
     * 获取红包
     * */
    @Select("select * from t_red_packet where id = #{id}")
    RedPacket getRedPacket(@Param("id") long id);

    /**
     *扣减红包
     * */
    @Update("update t_red_packet set stock = stock - 1 where id = #{id}")
    int decreaseRedPacket(@Param("id") long id);

    /**
     * 悲观锁
     * */
    @Select("select * from t_red_packet where id = #{id} for update")
    RedPacket getRedPacketByLock(@Param("id") long id);

    /**
     * 乐观锁
     * */
    @Update("update t_red_packet set stock = stock - 1, version = version + 1 where id = #{id} and version = #{version}")
    int decreaseRedPacketByVersion(@Param("id")long id, @Param("version")long version);
}
